package com.fashion.web.actions;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fashion.model.News;
import com.fashion.model.NewsColumn;
import com.fashion.service.ColumnService;
import com.fashion.service.NewsService;
import com.fashion.utils.NewsTemplate;
import com.opensymphony.xwork2.ActionSupport;

@Scope("prototype")
@Controller
@Namespace("/life")
public class LifeAction extends ActionSupport {
	List<News> focusNews = new ArrayList<News>();
	List<News> foodNews = new ArrayList<News>();
	List<News> specialNews = new ArrayList<News>();
	List<News> videoNews = new ArrayList<News>();
	List<News> golfNews = new ArrayList<News>();
	List<News> houseNews = new ArrayList<News>();
	List<News> holidayNews = new ArrayList<News>();
	List<News> digitalNews = new ArrayList<News>();
	List<News> furnitureNews = new ArrayList<News>();
	List<News> lifeSpecialNews = new ArrayList<News>();
	List<News> activityNews = new ArrayList<News>();
    private NewsColumn column;
    private static final Logger logger = Logger.getLogger(LifeAction.class);

    @Resource(name = "newsService")
	private NewsService newsService;
	@Resource(name = "columnService")
	private ColumnService columnService;

	@Action(value = "index", results = { @Result(name = SUCCESS, location = "index.jsp"), @Result(name = ERROR, location = "../error.jsp") })
	public String index() throws Exception {
        try {
            List<NewsTemplate> templates = new ArrayList<NewsTemplate>();
            // focus news
            templates.add(new NewsTemplate(5, 6));
            NewsColumn focusColumn = columnService.findColumnByUrl("life", "life");
            column = focusColumn;
            List<NewsColumn> focusColumns = columnService
                    .getAllChildren(focusColumn);
            focusColumns.add(focusColumn);
            focusNews = newsService.searchNewsByColumnListAndTemplateList(
                    focusColumns, templates, 1);
            // foodNews
            templates.clear();
            templates.add(new NewsTemplate(4, 2));
            templates.add(new NewsTemplate(3, 3));
            templates.add(new NewsTemplate(2, 6));
            NewsColumn foodColumn = columnService.findColumnByUrl("life", "meishi");
            List<NewsColumn> foodColumns = columnService.getAllChildren(foodColumn);
            NewsColumn wineColumn = columnService.findColumnByUrl("life", "meijiu");
            foodColumns.addAll(columnService.getAllChildren(wineColumn));
            foodColumns.add(foodColumn);
            foodColumns.add(wineColumn);
            foodNews = newsService.searchNewsByColumnListAndTemplateList(
                    foodColumns, templates, 1);
            // specialNews
            templates.clear();
            templates.add(new NewsTemplate(4, 1));
            templates.add(new NewsTemplate(3, 2));
            templates.add(new NewsTemplate(2, 3));
            NewsColumn specialColumn = columnService.findColumnByUrl("special",
                    "life");
            List<NewsColumn> specialColumns = columnService
                    .getAllChildren(specialColumn);
            specialColumns.add(specialColumn);
            specialNews = newsService.searchNewsByColumnListAndTemplateList(
                    specialColumns, templates, 1);
            templates.clear();
            NewsColumn lifeSpeciaColumn = columnService.findColumnByUrl("special",
                    "life");
            List<NewsColumn> lifeSpecialColumns = columnService
                    .getAllChildren(lifeSpeciaColumn);
            lifeSpecialColumns.add(lifeSpeciaColumn);
            templates.add(new NewsTemplate(4, 4));
            lifeSpecialNews = newsService.searchNewsByColumnListAndTemplateList(
                    lifeSpecialColumns, templates, 1);

            // videoNews
            templates.clear();
            templates.add(new NewsTemplate(4, 1));
            NewsColumn videoColumn = columnService.findColumnByUrl("video",
                    "jiudian");
            List<NewsColumn> videoColumns = columnService
                    .getAllChildren(videoColumn);
            videoColumns.add(videoColumn);
            videoNews = newsService.searchNewsByColumnListAndTemplateList(
                    videoColumns, templates, 1);
            // golfNews
            templates.clear();
            templates.add(new NewsTemplate(4, 2));
            templates.add(new NewsTemplate(3, 6));
            NewsColumn golfColumn = columnService
                    .findColumnByUrl("life", "gaoerfu");
            List<NewsColumn> golfColumns = columnService.getAllChildren(golfColumn);
            golfColumns.add(golfColumn);
            golfNews = newsService.searchNewsByColumnListAndTemplateList(
                    golfColumns, templates, 1);
            // houseNews
            templates.clear();
            templates.add(new NewsTemplate(4, 1));
            templates.add(new NewsTemplate(3, 2));
            templates.add(new NewsTemplate(2, 5));
            NewsColumn houseColumn = columnService.findColumnByUrl("life",
                    "haozhai");
            List<NewsColumn> houseColumns = columnService
                    .getAllChildren(houseColumn);
            houseColumns.add(houseColumn);
            houseNews = newsService.searchNewsByColumnListAndTemplateList(
                    houseColumns, templates, 1);
            // holidayNews
            templates.clear();
            templates.add(new NewsTemplate(4, 2));
            templates.add(new NewsTemplate(3, 4));
            templates.add(new NewsTemplate(2, 2));
            templates.add(new NewsTemplate(1, 12));
            NewsColumn holidayColumn = columnService.findColumnByUrl("life",
                    "dujia");
            List<NewsColumn> holidayColumns = columnService
                    .getAllChildren(holidayColumn);
            holidayColumns.add(holidayColumn);
            holidayNews = newsService.searchNewsByColumnListAndTemplateList(
                    holidayColumns, templates, 1);
            // digitalNews
            templates.clear();
            templates.add(new NewsTemplate(4, 1));
            templates.add(new NewsTemplate(3, 3));
            NewsColumn digitalColumn = columnService.findColumnByUrl("life",
                    "shangpin");
            List<NewsColumn> digitalColumns = columnService
                    .getAllChildren(digitalColumn);
            digitalColumns.add(digitalColumn);
            digitalNews = newsService.searchNewsByColumnListAndTemplateList(
                    digitalColumns, templates, 1);
            // furnitureNews
            templates.clear();
            templates.add(new NewsTemplate(4, 1));
            templates.add(new NewsTemplate(3, 3));
            NewsColumn furnitureColumn = columnService.findColumnByUrl("life",
                    "jiaju");
            List<NewsColumn> furnitureColumns = columnService
                    .getAllChildren(furnitureColumn);
            furnitureColumns.add(furnitureColumn);
            furnitureNews = newsService.searchNewsByColumnListAndTemplateList(
                    furnitureColumns, templates, 1);
            // 活动
            templates.clear();
            NewsColumn activityColumn = columnService.findColumnByUrl("activity",
                    "activity");
            List<NewsColumn> activityColumns = columnService
                    .getAllChildren(activityColumn);
            activityColumns.add(activityColumn);
            templates.add(new NewsTemplate(4, 2));
            templates.add(new NewsTemplate(3, 4));
            activityNews = newsService.searchNewsByColumnListAndTemplateList(
                    activityColumns, templates, 2);
        } catch (Exception e) {
            logger.error("exception in LifeAction",e);

            return ERROR;
        }

        return SUCCESS;
	}

	public List<News> getFocusNews() {
		return focusNews;
	}

	public List<News> getFoodNews() {
		return foodNews;
	}

	public List<News> getSpecialNews() {
		return specialNews;
	}

	public List<News> getVideoNews() {
		return videoNews;
	}

	public List<News> getGolfNews() {
		return golfNews;
	}

	public List<News> getHouseNews() {
		return houseNews;
	}

	public List<News> getHolidayNews() {
		return holidayNews;
	}

	public List<News> getDigitalNews() {
		return digitalNews;
	}

	public List<News> getFurnitureNews() {
		return furnitureNews;
	}

	public List<News> getLifeSpecialNews() {
		return lifeSpecialNews;
	}

	public List<News> getActivityNews() {
		return activityNews;
	}

    public NewsColumn getColumn() {
        return column;
    }

    public void setColumn(NewsColumn column) {
        this.column = column;
    }
}
